<?php

	include_once 'User.php';
	class UserDAL{
		const table_name = "blog_users";
		const class_name = "User";
		
		private $m_connection;
		
		public function __construct(DBConnection $DBConnection){
			$this->m_connection = $DBConnection;
		}
		
		public function GetAllInstances(){
			return $this->m_connection->GetAllInstances(UserDAL::class_name, UserDAL::table_name);
		}
		
		public function CreateTable() {
	      $sql = "CREATE TABLE `nisse` 
	      		(
	      			`pk` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
	      			`First` VARCHAR(255),
	      			`Second` VARCHAR(255),
	      			`Third` VARCHAR(255)
	      		)
	      		ENGINE = MyISAM;";				
	      
	      return $this->m_connection->RunQuery($sql);
    	}
		
		public function SelectBy($argument, $equals) {
			$sql = "SELECT * FROM " . UserDAL::table_name . " WHERE ($argument = ?)";
			return $this->m_connection->RunPreparedSelectQuery(UserDAL::class_name, $sql, "s", array(&$equals));
		}
		
		public function SelectByPk($pk) {
			$r = $this->SelectBy("pk", $pk);
			
			if (count($r) === 0) {
				return NULL;
			} else {
				return current($r);
			}
		}
	}
